package aperturescience.types;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

public class PatientState extends JSONArray {

	public PatientState() {
		super();
	}
	
	public PatientState(JSONArray a) {
		super();
		for (int i=0; i<a.length(); i++) try {
			super.put( i, a.get(i) );
		} catch (JSONException e) { continue; }
	}
	
	public boolean populateState(String state) {
		for(int index=0; index<super.length(); index++)
			super.remove(index);
		super.put(state);
		return true;
	}
	
	public boolean addState(String state) {
		super.put(state);
		return true;
	}
	
	public boolean removeState(String state) {
		for(int index=0; index<super.length(); index++)
			try {
				if (super.getString(index).equals(state)) {
					super.remove(index);
					return true;
				}
			} catch (JSONException e) { continue; }
		return false;
	}
	
	public boolean hasState(String state) {
		for(int index=0; index<super.length(); index++)
			try {
				if ( super.getString(index).equals(state) )
					return true;
			} catch (JSONException e) { continue; }
		return false;
	}
	
	public boolean transitionState(String oldState, String newState) {
		if ( removeState(oldState) )
			return addState(newState);
		return false;
	}
	
	public boolean transitionStates(List<String> oldStates, List<String> newStates) {
		if( oldStates.size() != newStates.size() )
			return false;
		for(int index = 0; index < oldStates.size(); index++)
			transitionState(oldStates.get(index), newStates.get(index));
		return true;
	}
}
